package com.tianyang.yukuaifu.business.main.view;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.main.adapter.ServiceManageAdapter;
import com.tianyang.yukuaifu.business.main.bean.PosAgentOutput;
import com.tianyang.yukuaifu.business.main.contract.ServiceManageContract;
import com.tianyang.yukuaifu.business.main.presenter.ServiceManagePresenter;
import com.tianyang.yukuaifu.business.main.provider.MainOpenProvider;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.jump.eventbus.RefreshServiceManageEvent;
import com.tianyang.yukuaifu.module.widget.VerticalSwipeRefreshLayout;
import com.tianyang.yukuaifu.utils.SoftInputUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;

/**
 * Description：服务商管理
 * Created by xyj on 2024/3/10
 */
public class ServiceManageActivity extends BaseTitleActivity implements ServiceManageContract.View {

    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;
    @BindView(R.id.emptyView)
    RelativeLayout emptyView;

    private ServiceManageAdapter vServiceManageAdapter;

    private ServiceManageContract.Presenter mPresenter;

    private int nowPage = CommonConfig.PAGE_NUM;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_merchant_manage;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("服务商管理");
        commonTitle.showRightImageView(R.drawable.ic_add_service);
        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        vServiceManageAdapter = new ServiceManageAdapter();
        vRecyclerview.setAdapter(vServiceManageAdapter);
        vServiceManageAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        vServiceManageAdapter.setPreLoadNumber(2);

        mPresenter = new ServiceManagePresenter(this);
        mPresenter.getServiceManageList(nowPage, phoneNumberEt.getText().toString());
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainOpenProvider.getInstance().openServiceRegisterActivity(getContext());
            }
        });
        phoneNumberEt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    SoftInputUtil.hideSoftInput(ServiceManageActivity.this);
                    nowPage = CommonConfig.PAGE_NUM;
                    mPresenter.getServiceManageList(nowPage, phoneNumberEt.getText().toString());
                    vServiceManageAdapter.setEnableLoadMore(true);
                }
                return false;
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = CommonConfig.PAGE_NUM;
                mPresenter.getServiceManageList(nowPage, phoneNumberEt.getText().toString());
                vServiceManageAdapter.setEnableLoadMore(true);
            }
        });
        vServiceManageAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getServiceManageList(nowPage, phoneNumberEt.getText().toString());
            }
        }, vRecyclerview);
        vServiceManageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.updateTypeRtv) {
                    PosAgentOutput output = ((ServiceManageAdapter)adapter).getItem(position);
                    if (output != null)
                        MainOpenProvider.getInstance().openRateConfigActivity(
                                mContext, output.getPhone(), 1,true);
                }
            }
        });
    }

    @Override
    public void showServiceManageListView(List<PosAgentOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size() > 0) {
            if (isFirst) {
                vServiceManageAdapter.setNewData(outputs);
            } else {
                vServiceManageAdapter.addData(outputs);
            }
            nowPage++;
            vRecyclerview.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
        } else if (isFirst) {
            vRecyclerview.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vServiceManageAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vServiceManageAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vServiceManageAdapter.loadMoreFail();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshServiceManageEvent(RefreshServiceManageEvent event) {
        if (event != null) {
            nowPage = CommonConfig.PAGE_NUM;
            phoneNumberEt.setText("");
            mPresenter.getServiceManageList(nowPage, phoneNumberEt.getText().toString());
            vServiceManageAdapter.setEnableLoadMore(true);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(ServiceManageContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
